# pragma once

# include<Arduino.h>
# include<Ticker.h>
# include "../pin_list.hpp"
# include "../global.hpp"
# include "../io_action_config.hpp"

namespace IDPDSW
{
    const int sig[3] {D5, D6, D7};
    const String name[3] {"sig0", "sig1", "sig2"};
    int state[3];
    Esp8266OutputPin sw0 (D2, "sw0");
    
    Ticker idpd_sw_loop_ticker;

    void read_state(int buf[3]) {
        for (int i=0; i<3; i++) {
            buf[i] = digitalRead(sig[i]);
        }
    }

    void idpd_sw_loop() {
        int buf[3]; read_state(buf);
        for (int i=0; i<3; i++) {
            if (buf[i] != state[i]) {
                state[i] = buf[i];
                trigger_input_pin(name[i], buf[i]);
            }
        }
    }
    
    void init() {
        if (!WORKER_IS_IDPDSW) return ;
        output_pin_list.push_back(static_cast<AbstructOutputPin*> (&sw0));

        for (int pin : sig) {
            pinMode(pin, INPUT);
        }
        
        read_state(state);
        idpd_sw_loop_ticker.attach_ms_scheduled(50, idpd_sw_loop);

        Serial.println(WORKER_NAME + " inited. In idpd_sw.hpp.");
    }
} // namespace IDPDSW
